package com.afkt.project.ui.slice;


import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseButton;
import com.dev.base.widget.BaseEditText;
import com.dev.base.widget.BaseImageView;
import com.dev.other.ZXingQRCodeUtils;
import com.dev.utils.app.*;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.Text.TextObserver;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Environment;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;

import java.io.*;


public class QRCodeCreateSlice extends BaseActivity implements Component.ClickedListener {

    private BaseButton vidAqcCreateBtn;

    private BaseButton vidAqcSelectBtn;

    private BaseEditText vidAqcContentEdit;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;

    private static final String TAG = QRCodeCreateSlice.class.getSimpleName();
    private BaseImageView vidAqcIgview;

    // 图片 Bitmap
    PixelMap selectBitmap;


    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_qrcode_create;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });

        ListenerUtils.setOnClicks(this, vidAqcCreateBtn, vidAqcSelectBtn);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("创建二维码");
        bar_Layout.addComponent(bar);
        vidAqcContentEdit = (BaseEditText) findComponentById(ResourceTable.Id_vid_aqc_content_edit);
        vidAqcIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_aqc_igview);
        vidAqcSelectBtn = (BaseButton) findComponentById(ResourceTable.Id_vid_aqc_select_btn);
        vidAqcCreateBtn = (BaseButton) findComponentById(ResourceTable.Id_vid_aqc_create_btn);

        vidAqcContentEdit.addTextObserver((text1, start, before, count) -> {
            if(vidAqcContentEdit.getText()!=null){
                if (vidAqcContentEdit.getText().length() > 120) {
                    vidAqcContentEdit.setText(vidAqcContentEdit.getText().substring(0,120));
                }
            }
        });
        PixelMapElement element = ResourceUtils.getDrawable(ResourceTable.Media_icon);
    }


    @Override
    public void onClick(Component component) {
        String text = EditTextUtils.getText(vidAqcContentEdit);
        if (text == null || text.length() == 0) {
            showToast(false, "请输入生成二维码内容");
            return;
        }
        int size = SizeUtils.dipConvertPx(1000f);
        switch (component.getId()) {
            case ResourceTable.Id_vid_aqc_create_btn:


                LogUtils.error(TAG, "onClick ");
                ZXingQRCodeUtils.createQRCodeImage(text, size, selectBitmap, (success, bitmap, e) -> {
                    LogUtils.error(TAG, "success = " + success);
                    if (success) {
                        HandlerUtils.postRunnable(() -> {
                            LogUtils.error(TAG, "bitmap = " + bitmap);
                            ImageViewUtils.setImagePixelMap(vidAqcIgview, bitmap);
                            setImageData(bitmap, System.currentTimeMillis() + "");

                        });
                    } else {
                        LogUtils.error(TAG, e.getMessage());
                        showToast(false, e.getMessage());
                    }
                });

                break;

            case ResourceTable.Id_vid_aqc_select_btn:
                // 初始化图片配置
                ZXingQRCodeUtils.createQRCodeImage(text, size, selectBitmap, (success, bitmap, e) -> {
                    LogUtils.error(TAG, "success = " + success);
                    if (success) {
                        HandlerUtils.postRunnable(() -> {
                            LogUtils.error(TAG, "bitmap = " + bitmap);
                            PixelMap logoPixelMap = ZXingQRCodeUtils.addLogoToQRCode(bitmap, ((Image) findComponentById(ResourceTable.Id_logo)).getPixelMap());
                            ImageViewUtils.setImagePixelMap(vidAqcIgview, logoPixelMap);
                            setImageData(logoPixelMap, System.currentTimeMillis() + "");

                        });
                    } else {
                        LogUtils.error(TAG, e.getMessage());
                        showToast(false, e.getMessage());
                    }
                });

                break;
        }
    }

    public void showToast(Boolean success, String text) {
        if (success) {
            ToastTintUtils.success(text);
        } else {
            ToastTintUtils.error(text);
        }
    }

    private void setImageData(PixelMap bitmap, String name) {
        ImagePacker imagePacker = ImagePacker.create();
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), name + ".png");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            //   HiLog.info(LABEL_LOG, LOG_FORMAT, "", "out put file path is incorrect");
        }
        ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
        packingOptions.format = "image/png";
        packingOptions.quality = 100;// 设置图片质量
        boolean result = imagePacker.initializePacking(outputStream, packingOptions);
        LogUtils.error(TAG, "result = " + result);
        result = imagePacker.addImage(bitmap);
        long dataSize = imagePacker.finalizePacking();
        LogUtils.error(TAG, "dataSize = " + dataSize);
    }


}
